<template>
  <div class="outsourcingList">
    <jc-button
      :btn-arr="['reload', bomCost('workingName:insert') && 'newy', 'remove', 'pass', 'sub', 'option']"
      :disab-arr="['close', 'cancel', 'com1', 'com2', 'copy', 'freeze']"
      :fbillno="fbillNo"
      @refresh="handleGetTable"
      @newly="newAssem"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @forbidden="using(1)"
      @startUsing="using(2)"
      @toExcel="handleExport"
      @del="delectHandle"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleSearch"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleSearch">
          {{ $t('btn.search') }}
        </el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleSearch"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        sortable
        table-selection
        get-row-keys="fentryId"
        @selectionChange="handleSelectAll"
      >
        <!--审核状态-->
        <template v-slot:btnStates="col">
          <span v-if="col.scope.row.fdocumentstatus === ''" />
          <el-tag
            v-else
            :type="col.scope.row.stateColor"
          >
            {{ col.scope.row.fdocumentstatus }}
          </el-tag>
        </template>
        <!--关闭状态-->
        <template v-slot:btnState="col">
          <el-tag
            v-if="col.scope.row.fforbidstatus === 'A'"
          >
            {{ $t('market.normal') }}
          </el-tag>
          <el-tag v-else type="danger">{{ $t('btn.disab') }}<!--禁用--></el-tag>
        </template>
        <template v-slot:btnSlot="col">
          <el-button
            type="warning"
            size="mini"
            :disabled="col.scope.row.fdocumentstatus==='审核中' || col.scope.row.fdocumentstatus==='已审核'"
            @click="newAssem(col.scope.row)"
          >{{ $t('btn.edit') }}</el-button>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="handleGetTable"
      />
    </div>
    <!--  新增外包工序  -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.outProcess')"
      :visible.sync="openProcess"
      :close-on-click-modal="false"
      close-on-press-escape
      width="500px"
      top="5vh"
    >
      <div class="preview">
        <el-form :model="process" label-width="100px" size="mini">
          <el-form-item :label="$t('market.processname')">
            <el-input v-model="process.fnumber" />
          </el-form-item>
          <el-form-item :label="$t('market.processnumber')">
            <el-input v-model="process.fname" />
          </el-form-item>
          <el-form-item :label="$t('shop.fnumber1')">
            <el-input v-model="process.fmaterialnumber">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="isMateria=true"
              />
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('market.processModel')">
            <el-input v-model="process.fmnemoniccode" />
          </el-form-item>
          <el-form-item :label="$t('market.note')">
            <el-input v-model="process.fnote" />
          </el-form-item>
          <el-form-item>
            <el-button @click="openProcess=false">{{ $t('btn.cancel') }}</el-button>
            <el-button type="primary" @click="proSubmit">{{ $t('btn.save') }}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <!--    物料弹窗-->
    <material v-if="isMateria" @material="materialData" />
  </div>
</template>

<script>
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import {
  queryWorkingNameList,
  workingNameAudit,
  workingNameUnAudit,
  workingNameUnSubmit,
  workingNameSubmit,
  deleteWorkingName,
  workingNameDisable,
  workingNameUnDisable,
  insertWorkingName,
  updateWorkingName
} from '@/api/workshop'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import { notoMxBmina, toMxBmina } from '@/components/ToMxamineState'
import jcButton from '@/components/Button'

export default {
  name: 'OutsourcingList',
  components: {
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material'),
    search,
    filtration,
    jcButton
  },
  mixins: [searData],
  data() {
    return {
      isMateria: false,
      openProcess: false,
      process: {
        fnumber: '',
        fname: '',
        fmnemoniccode: ''
      },
      ftype: 73,
      fbillNo: '', // 编码
      total: 0, // 总条目
      pageNum: 1, // 当前页
      pageSize: 200, // 每页显示多少条数据
      // 表头
      tableHeader: [
        { label: this.$t('market.processModel'), prop: 'fmnemoniccode' },
        { label: this.$t('market.processname'), prop: 'fnumber' },
        { label: this.$t('market.processnumber'), prop: 'fname' },
        { label: this.$t('market.documentstatus'), type: 'states', width: '90' },
        { label: this.$t('market.closeState'), type: 'state', prop: 'fclosestatus', width: '90' },
        { label: this.$t('market.fmodel'), prop: 'fmodel' },
        { label: this.$t('shop.fnumber1'), prop: 'fmaterialnumber' },
        { label: this.$t('shop.fname2'), prop: 'fmaterialname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescription' },
        { label: this.$t('market.note'), prop: 'fnote' },
        { label: this.$t('market.creator'), prop: 'fcreator', width: '100' },
        { label: this.$t('market.createtime'), prop: 'fcreatedate' },
        { label: this.$t('market.handle'), type: 'btn', width: '100' }
      ],
      tableHeight: '72vh',
      // 表格数据
      tableData: [],
      listDate: [],
      tableHeader1: [],
      selectAll: [] // 全选数据
    }
  },
  created() {
    this.handleGetTable()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 240px)' : '72vh'
  },
  methods: {
    // 导出表格-其它入库单
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.selectAll && this.selectAll.length > 0) {
          const list = this.selectAll
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '外包工序列表')
        } else {
          queryWorkingNameList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array.map(item => {
                return toMxBmina(item)
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '外包工序列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取列表数据
    async handleGetTable(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const res = await queryWorkingNameList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        search: this.searCollData.concat(this.details)
      })
      const { code, data, message } = res
      if (code !== 0) {
        this.$message.warning(message)
        return
      }
      this.tableData = data.array.map(item => {
        return toMxBmina(item)
      })
      this.total = data.total
      this.loading = false
    },
    // 获取物料
    materialData(item) {
      if (item.fnumber) {
        this.process.fmaterialnumber = item.fnumber
        this.process.fmaterialid = item.fmaterialId
      }
      this.isMateria = false
    },
    // 全选
    handleSelectAll(item) {
      this.selectAll = item
      this.fbillNo = item[0].fbillNo
      this.listDate = []
      item.forEach(item => {
        item.fnumber && this.listDate.push(item.fnumber)
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.listDate }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.listDate = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
        this.handleGetTable()
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? workingNameSubmit : workingNameUnSubmit
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? workingNameAudit : workingNameUnAudit
      this.toExamineFun(api)
    },
    // 禁用
    async using(ev) {
      const api = ev === 1 ? workingNameDisable : workingNameUnDisable
      this.toExamineFun(api)
    },
    // 删除
    delectHandle() {
      this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {
        const api = deleteWorkingName
        this.toExamineFun(api)
      })
    },
    // 新增外包工序
    newAssem(ev) {
      this.openProcess = true
      this.process = {
        fnumber: '',
        fname: '',
        ...ev
      }
    },
    // 保存 修改工序
    proSubmit() {
      const api = this.process.fid ? updateWorkingName : insertWorkingName
      api({ ...notoMxBmina(this.process) }).then(res => {
        if (res.code === 0) {
          this.handleGetTable()
          this.openProcess = false
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 搜索
    handleSearch(ev) {
      this.pageNum = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetTable(ev.detail)
      } else {
        this.handleGetTable()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.outsourcingList {
  @include listBom;
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
.success{
  color: #67c23a;
}
.warning{
  color: #e6c23c;
}
.fbillno {
  color: #409eff;
  &:hover {
    text-decoration: underline;
    cursor: pointer;
  }
}
</style>
